﻿using System;
using System.Collections.Generic;
using System.Data;
using br.com.SistemaChamados.Enum;
using br.com.SistemaChamados.Model;
using br.com.SistemaChamados.QueryManager.Core;
using br.com.SistemaChamados.QueryManager.Core.Behavior;

namespace br.com.SistemaChamados.DAO
{
    public class UnitDAO : GenericDao<Unit>
    {
        private static string SQL_INSERT_UNIT = " INSERT INTO TBL_UNIT (TBL_UNITNAME, TBL_PLACEID) VALUES (@name, @placeid) ";
        private static string SQL_UPDATE_UNIT = " UPDATE TBL_UNIT SET TBL_UNITNAME = @name WHERE TBL_UNITID = @id ";
        private static string SQL_DELETE_UNIT = " DELETE FROM TBL_UNIT WHERE TBL_UNITID = @id ";

        public static Unit getUnit(int unitId)
        {
            Unit unitReturn = new Unit();
            try
            {
                unitReturn = (Unit)QueryManagerPerformer.Select(typeof(Unit), "Id", unitId, ConnectionBehavior.CONNECTION_BEHAVIOR_CLOSE_AFTER_OPERATION);
            }
            catch (Exception ex)
            {
                unitReturn = null;
                throw ex;
            }
            return unitReturn;
        }

        public static List<Unit> getUnits()
        {
            List<Unit> list = new List<Unit>();
            try
            {
                list = QueryManagerPerformer.Select(typeof(Unit), ConnectionBehavior.CONNECTION_BEHAVIOR_CLOSE_AFTER_OPERATION)
                        .ConvertAll(convertObject);
            }
            catch (Exception ex)
            {
                list = null;
                throw ex;
            }
            return list;
        }

        public static EnumCRUDState InsertUnit(Unit unit)
        {
            EnumCRUDState stateTransaction = EnumCRUDState.SQL_NOTHING;
            IDbConnection conn = null;
            IDbCommand command = null;
            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_INSERT_UNIT, conn);
                command.Parameters.Add(Mapped.Parameter("@name", unit.Name));
                command.Parameters.Add(Mapped.Parameter("@placeid", unit.Place.Id));
                command.ExecuteNonQuery();
                stateTransaction = EnumCRUDState.SQL_SUCCESS;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
            return stateTransaction;
        }

        public static EnumCRUDState UpdateUnit(Unit unit)
        {
            EnumCRUDState stateTransaction = EnumCRUDState.SQL_NOTHING;
            IDbConnection conn = null;
            IDbCommand command = null;
            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_UPDATE_UNIT, conn);
                command.Parameters.Add(Mapped.Parameter("@name", unit.Name));
                command.Parameters.Add(Mapped.Parameter("@id", unit.Id));
                command.ExecuteNonQuery();
                stateTransaction = EnumCRUDState.SQL_SUCCESS;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
            return stateTransaction;
        }

        public static EnumCRUDState DeleteUnit(Unit unit)
        {
            EnumCRUDState stateTransaction = EnumCRUDState.SQL_NOTHING;
            IDbConnection conn = null;
            IDbCommand command = null;
            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_DELETE_UNIT, conn);
                command.Parameters.Add(Mapped.Parameter("@id", unit.Id));
                command.ExecuteNonQuery();
                stateTransaction = EnumCRUDState.SQL_SUCCESS;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

            return stateTransaction;
        }
    }
}